package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.dao.SubSectorDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.SubSector;
import org.springframework.stereotype.Repository;

@Repository
public class SubSectorDaoImpl extends AbstractDaoImpl<SubSector, Integer>
		implements SubSectorDao {

	protected SubSectorDaoImpl() {
		super(SubSector.class);
	}

	@Override
	public void saveSubSector(SubSector subSector) {
		saveOrUpdate(subSector);

	}

	@Override
	public List<SubSector> findSubSectors(String subSectorName) {
		return findByCriteria(Restrictions.like("name", subSectorName, MatchMode.START));
	}

	@Override
	public List<SubSector> searchSubSector(int sectorId, String code) {
		Criteria criteria = getCurrentSession().createCriteria(
				SubSector.class);
		if (sectorId != -1) {
			criteria.add(Restrictions.eq("sector.id",sectorId));
		}
		if (code != null) {
			criteria.add(Restrictions.like("code", code , MatchMode.START));
		}
		return findByCriteria(criteria);
	}

	@Override
	public Page<SubSector> searchSubSector(int sectorId, String code,
			GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				SubSector.class);
		if (sectorId != -1) {
			criteria.add(Restrictions.like("sector.id", sectorId));
		}
		if (code != null) {
			criteria.add(Restrictions.like("code", code , MatchMode.START));
		}
		return findByCriteria(criteria , gridPramDTO);
	}

}
